package com.irdstudio.bsp.executor.core.assembly.license.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 01.系统注册授权信息(s_inst_license)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-07-13
 */
public class SInstLicenseDao {

	private static final Logger logger = LoggerFactory.getLogger(SInstLicenseDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public SInstLicenseDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public SInstLicense queryWithKeys(String sysInstId)
			throws SQLException {
		SInstLicense dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM s_inst_license WHERE sys_inst_id=?");
			ps.setString(1,sysInstId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new SInstLicense();
				dc.setSysInstId(rs.getString("sys_inst_id"));
				dc.setHostName(rs.getString("host_name"));
				dc.setWebPort(rs.getString("web_port"));
				dc.setSysCode(rs.getString("sys_code"));
				dc.setSysName(rs.getString("sys_name"));
				dc.setAuthTarget(rs.getString("auth_target"));
				dc.setAuthBeginDate(rs.getString("auth_begin_date"));
				dc.setAuthEndDate(rs.getString("auth_end_date"));
				dc.setRemark(rs.getString("remark"));
			}
		} catch (SQLException e) {
			throw new SQLException("querySInstLicenseWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * 
	 * 该代码需重新实现， 必须使用prepareStatement的绑定， 不能简单进行sql的拼接！
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<SInstLicense> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<SInstLicense> dcList = new ArrayList<SInstLicense>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM s_inst_license "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				SInstLicense dc = new SInstLicense();
//				dc.setSysInstId(rs.getString("sys_inst_id"));
//				dc.setHostName(rs.getString("host_name"));
//				dc.setWebPort(rs.getString("web_port"));
//				dc.setSysCode(rs.getString("sys_code"));
//				dc.setSysName(rs.getString("sys_name"));
//				dc.setAuthTarget(rs.getString("auth_target"));
//				dc.setAuthBeginDate(rs.getString("auth_begin_date"));
//				dc.setAuthEndDate(rs.getString("auth_end_date"));
//				dc.setRemark(rs.getString("remark"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("querySInstLicenseWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}
	
	/**
	 * 注册系统实例(先清除相同实例ID后再写入)
	 * @param instLicense
	 */

	public boolean registerInst(SInstLicense instLicense) {
		boolean bFlag = false;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement("DELETE FROM s_inst_license WHERE sys_inst_id=?");
			ps.setString(1, instLicense.getSysInstId());
			ps.execute();
			ps.close();
			ps = conn
					.prepareStatement("INSERT INTO s_inst_license(SYS_INST_ID,HOST_NAME,WEB_PORT,SYS_CODE,SYS_NAME,AUTH_TARGET,AUTH_BEGIN_DATE,AUTH_END_DATE,REMARK) VALUES(?,?,?,?,?,?,?,?,?)");
			ps.setString(1, instLicense.getSysInstId());
			ps.setString(2, instLicense.getHostName());
			ps.setString(3, instLicense.getWebPort());
			ps.setString(4, instLicense.getSysCode());
			ps.setString(5, instLicense.getSysName());
			ps.setString(6, instLicense.getAuthTarget());
			ps.setString(7, instLicense.getAuthBeginDate());
			ps.setString(8, instLicense.getAuthEndDate());
			ps.setString(9, instLicense.getRemark());
			ps.execute();
			bFlag = true;
		} catch (SQLException e) {
			logger.error("注册系统实例失败", e);
		} finally {
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					logger.error("关闭prepareStatement失败", e);
				}
		}
		return bFlag;
	}	

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}
}
